home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / c / agl103p.lha / src / agl / RCS / rgb.c,v < prev    next >
Encoding:
Text File  |  1994-12-09  |  7.9 KB  |  521 lines

  1. head    1.2;
  2. branch    1.2.1.99;
  3. access;
  4. symbols;
  5. locks; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    94.09.17.05.54.04;    author jason;    state Exp;
  11. branches
  12.     1.2.1.1;
  13. next    ;
  14.  
  15. 1.2.1.1
  16. date    94.11.16.06.57.48;    author jason;    state Exp;
  17. branches;
  18. next    1.2.1.2;
  19.  
  20. 1.2.1.2
  21. date    94.12.09.05.29.56;    author jason;    state Exp;
  22. branches;
  23. next    ;
  24.  
  25.  
  26. desc
  27. @RGB truecolor functions
  28. @
  29.  
  30.  
  31. 1.2
  32. log
  33. @initial revision
  34. @
  35. text
  36. @
  37. /******************************************************************************
  38.  
  39. $Id: $
  40.  
  41. $Log: $
  42.  
  43. ******************************************************************************/
  44.  
  45.  
  46. #ifndef NOT_EXTERN
  47. #include"agl.h"
  48. #endif
  49.  
  50. #define PATTERNSIZE (5*16)
  51.  
  52. short Dispersion[4][4]=
  53.     {
  54.      0, 12,  3, 15,
  55.      8,  4, 11,  7,
  56.      2, 14,  1, 13,
  57.     10,  6,  9,  5,
  58.     };
  59.  
  60. short DitherMatrix[16][16];
  61.  
  62.  
  63. /* 5 planes, 16 lines */
  64. UWORD RGBPattern[PATTERNSIZE];
  65.  
  66.  
  67. /******************************************************************************
  68. void    initialize_RGB(void)
  69.  
  70. ******************************************************************************/
  71. /*PROTOTYPE*/
  72. void initialize_RGB(void)
  73.     {
  74.     short x,y;
  75.  
  76.     for(y=0;y<16;y++)
  77.         {
  78.         for(x=0;x<16;x++)
  79.             DitherMatrix[y][x]=Dispersion[y/4][x/4]+Dispersion[y&3][x&3]*16;
  80.  
  81.         Expo[y]=1<<y;
  82.         }
  83.  
  84.     memset(RGBPattern,0,PATTERNSIZE*sizeof(UWORD));
  85.     }
  86.  
  87.  
  88. /******************************************************************************
  89. void    c3f(float rgb[3])
  90.  
  91. ******************************************************************************/
  92. /*PROTOTYPE*/
  93. void c3f(float rgb[3])
  94.     {
  95.     create_pattern(RGBPattern,rgb);
  96.     activate_pattern(RGBPattern);
  97.     }
  98.  
  99.  
  100. /******************************************************************************
  101. void    create_pattern(float *pattern,float rgb[3])
  102.  
  103. ******************************************************************************/
  104. /*PROTOTYPE*/
  105. void create_pattern(float *pattern,float rgb[3])
  106.     {
  107.     short plane,x,y;
  108.     short component;
  109.     
  110.     for(plane=0;plane<3;plane++)
  111.         {
  112.         component=255*rgb[plane];
  113.  
  114.         for(y=0;y<16;y++)
  115.             {
  116.             pattern[plane*16+y]=0;
  117.  
  118.             for(x=0;x<16;x++)
  119.                 if(DitherMatrix[y][x]<component)
  120.                     pattern[plane*16+y]+=Expo[x];
  121.             }
  122.         }
  123.     }
  124.  
  125.  
  126. /******************************************************************************
  127. void    activate_pattern(float *pattern)
  128.  
  129.     NULL pattern should deactivate current pattern
  130. ******************************************************************************/
  131. /*PROTOTYPE*/
  132. void activate_pattern(float *pattern)
  133.     {
  134.     SetAfPt(DrawRPort,pattern,-4);
  135.     }
  136. @
  137.  
  138.  
  139. 1.2.1.1
  140. log
  141. @added space diffusion
  142. @
  143. text
  144. @d4 1
  145. a4 1
  146. $Id: rgb.c,v 1.3 1994/11/16 06:29:26 jason Exp jason $
  147. d6 1
  148. a6 4
  149. $Log: rgb.c,v $
  150.  * Revision 1.2  1994/09/17  05:54:04  jason
  151.  * initial revision
  152.  *
  153. a14 10
  154. #define SPACE_DEBUG        FALSE
  155.  
  156.  
  157. #define DISPERSION        0
  158. #define SPACE_DIFFUSION    1
  159.  
  160. #define HALFTONE    SPACE_DIFFUSION
  161.  
  162.  
  163. /* 5 planes, 16 lines */
  164. a16 1
  165.  
  166. a24 13
  167.  
  168. long SpaceDiffusion[8][8]=
  169.     {
  170.     21, 22, 25, 26, 37, 38, 41, 42,
  171.     20, 23, 24, 27, 36, 39, 40, 43,
  172.     19, 18, 29, 28, 35, 34, 45, 44,
  173.     16, 17, 30, 31, 32, 33, 46, 47,
  174.     15, 12, 11, 10, 53, 52, 51, 48,
  175.     14, 13,  8,  9, 54, 55, 50, 49,
  176.      1,  2,  7,  6, 57, 56, 61, 62,
  177.      0,  3,  4,  5, 58, 59, 60, 63,
  178.     };
  179.  
  180. a26 21
  181. float Index_Colors[16][3]=
  182.     {
  183.      0/15.0,  0/15.0,  0/15.0,
  184.     15/15.0,  0/15.0,  0/15.0,
  185.      0/15.0, 15/15.0,  0/15.0,
  186.     15/15.0, 15/15.0,  0/15.0,
  187.      0/15.0,  0/15.0, 15/15.0,
  188.     15/15.0,  0/15.0, 15/15.0,
  189.      0/15.0, 15/15.0, 15/15.0,
  190.     15/15.0, 15/15.0, 15/15.0,
  191.  
  192.      5/15.0,  5/15.0,  5/15.0,
  193.     12/15.0,  7/15.0,  7/15.0,
  194.      7/15.0, 12/15.0,  7/15.0,
  195.      8/15.0,  8/15.0,  3/15.0,
  196.      7/15.0,  7/15.0, 12/15.0,
  197.      8/15.0,  3/15.0,  8/15.0,
  198.      3/15.0,  8/15.0,  8/15.0,
  199.     10/15.0, 10/15.0, 10/15.0,
  200.     };
  201.  
  202. d28 1
  203. a30 3
  204. long Expo[16];
  205. long DotClass[64][2];
  206.  
  207. a40 7
  208.     for(y=0;y<8;y++)
  209.         for(x=0;x<8;x++)
  210.             {
  211.             DotClass[SpaceDiffusion[y][x]][0]=x;
  212.             DotClass[SpaceDiffusion[y][x]][1]=y;
  213.             }
  214.  
  215. d66 1
  216. a66 3
  217. float    index_dist(short index,float rgb[3])
  218.  
  219.     return square of distance to index in color space
  220. a67 1
  221.     Note: sqrt() unneccasary since just finding minimum
  222. d70 1
  223. a70 1
  224. float index_dist(short index,float rgb[3])
  225. d72 1
  226. a72 26
  227.     float dx,dy,dz;
  228.  
  229.     dx=rgb[0]-Index_Colors[index][0];
  230.     dy=rgb[1]-Index_Colors[index][1];
  231.     dz=rgb[2]-Index_Colors[index][2];
  232.  
  233.     return    dx*dx+dy*dy+dz*dz;
  234.     }
  235.  
  236.  
  237. /******************************************************************************
  238. void    create_pattern(UWORD *pattern,float rgb[3])
  239.  
  240. ******************************************************************************/
  241. /*PROTOTYPE*/
  242. void create_pattern(UWORD *pattern,float rgb[3])
  243.     {
  244.     float mod[8][8][3],error[3];
  245.     float add,total;
  246.     float red,green,blue;
  247.     float best_dist,new_dist;
  248.  
  249.     short class;
  250.     short contribution;
  251.     short best_index,new_index;
  252.     short plane,x,y,i,j,m,pass;
  253. d75 1
  254. a75 1
  255.     if(HALFTONE==SPACE_DIFFUSION)
  256. d77 1
  257. a77 12
  258.         /* initialize */
  259.         for(y=0;y<8;y++)
  260.             for(x=0;x<8;x++)
  261.                 {
  262.                 mod[y][x][0]=rgb[0];
  263.                 mod[y][x][1]=rgb[1];
  264.                 mod[y][x][2]=rgb[2];
  265.                 }
  266.  
  267.         for(plane=0;plane<5;plane++)
  268.             for(y=0;y<16;y++)
  269.                 pattern[plane*16+y]=0;
  270. d79 1
  271. a79 1
  272.         for(class=0;class<64;class++)
  273. d81 1
  274. a81 2
  275.             x=DotClass[class][0];
  276.             y=DotClass[class][1];
  277. d83 3
  278. a85 110
  279.             if(SpaceDiffusion[y][x]!=class)
  280.                 GL_error("Class mismatch in create_pattern()");
  281.  
  282.             red=mod[y][x][0];
  283.             green=mod[y][x][1];
  284.             blue=mod[y][x][2];
  285.  
  286.             /* find nearest match */
  287.             best_index= ((((blue>0.5)<<1)+(green>0.5))<<1) + (red>0.5);
  288.             best_dist=index_dist(best_index,mod[y][x]);
  289.  
  290. #if SPACE_DEBUG
  291.             printf("%d: %4.2f,%4.2f,%4.2f %d(%.6G) ",class,red,green,blue,best_index,best_dist);
  292. #endif
  293.  
  294.             /* compare with middle colors */
  295.             j=0;
  296.             for(m=0;m<4;m++)
  297.                 {
  298.                 switch(m)
  299.                     {
  300.                     case 0:
  301.                         if(red>0.5)
  302.                             {
  303.                             new_index=PINK;
  304.                             j++;
  305.                             }
  306.                         else
  307.                             new_index=BLUEGREEN;
  308.                         break;
  309.                     case 1:
  310.                         if(green>0.5)
  311.                             {
  312.                             new_index=LIGHTGREEN;
  313.                             j++;
  314.                             }
  315.                         else
  316.                             new_index=PURPLE;
  317.                         break;
  318.                     case 2:
  319.                         if(blue>0.5)
  320.                             {
  321.                             new_index=LAVENDER;
  322.                             j++;
  323.                             }
  324.                         else
  325.                             new_index=OLIVE;
  326.                         break;
  327.                     case 3:
  328.                         if(j>1)
  329.                             new_index=LIGHTGREY;
  330.                         else
  331.                             new_index=DARKGREY;
  332.                         break;
  333.                     }
  334.  
  335. #if SPACE_DEBUG
  336.                 printf("%d.",new_index);
  337. #endif
  338.  
  339.                 new_dist=index_dist(new_index,mod[y][x]);
  340.                 if(best_dist>new_dist)
  341.                     {
  342.                     best_dist=new_dist;
  343.                     best_index=new_index;
  344.  
  345. #if SPACE_DEBUG
  346.                     printf("-> %d(%.6G) ",best_index,best_dist);
  347. #endif
  348.                     }
  349.                 }
  350.  
  351.             /* set index */
  352.             for(plane=0;plane<5;plane++)
  353.                 if(best_index&Expo[plane])
  354.                     {
  355.                     pattern[plane*16+y  ]+=Expo[x];
  356.                     pattern[plane*16+y+8]+=Expo[x];
  357.  
  358.                     pattern[plane*16+y  ]+=Expo[x+8];
  359.                     pattern[plane*16+y+8]+=Expo[x+8];
  360.                     }
  361.  
  362.             error[0]= mod[y][x][0]-Index_Colors[best_index][0];
  363.             error[1]= mod[y][x][1]-Index_Colors[best_index][1];
  364.             error[2]= mod[y][x][2]-Index_Colors[best_index][2];
  365.  
  366. #if SPACE_DEBUG
  367.             printf("e %4.2f,%4.2f,%4.2f\n",error[0],error[1],error[2]);
  368. #endif
  369.             
  370.             total=0;
  371.             for(pass=0;pass<2;pass++)
  372.                 for(j= -1;j<2;j++)
  373.                     for(i= -1;i<2;i++)
  374.                         if( !( (i==0 && j==0) ||    (i<0 && x==0) || (i>0 && x==7) ||
  375.                                                     (j<0 && y==0) || (j>0 && y==7) ||
  376.                                                     SpaceDiffusion[y+j][x+i]<class ) )
  377.                             {
  378.                             contribution= 1 + (i==0 || j==0);
  379.  
  380.                             if(pass==0)
  381.                                 total+=contribution;
  382.                             else
  383.                                 for(m=0;m<3;m++)
  384.                                     {
  385.                                     add= error[m] * contribution/total;
  386.                                     mod[y+j][x+i][m]+=add;
  387.                                     }
  388.                             }
  389. a86 4
  390.  
  391. #if SPACE_DEBUG
  392.         exit(1);
  393. #endif
  394. a87 14
  395.     else
  396.         for(plane=0;plane<3;plane++)
  397.             {
  398.             component=255*rgb[plane];
  399.  
  400.             for(y=0;y<16;y++)
  401.                 {
  402.                 pattern[plane*16+y]=0;
  403.  
  404.                 for(x=0;x<16;x++)
  405.                     if(DitherMatrix[y][x]<component)
  406.                         pattern[plane*16+y]+=Expo[x];
  407.                 }
  408.             }
  409. d92 1
  410. a92 1
  411. void    activate_pattern(UWORD *pattern)
  412. d97 1
  413. a97 1
  414. void activate_pattern(UWORD *pattern)
  415. a99 3
  416.     SetAPen(DrawRPort,255);
  417.     SetBPen(DrawRPort,0);
  418.     SetDrMd(DrawRPort,JAM2);
  419. @
  420.  
  421.  
  422. 1.2.1.2
  423. log
  424. @added copyright
  425. @
  426. text
  427. @d1 1
  428. d4 1
  429. a4 4
  430. Copyright © 1994 Jason Weber
  431. All Rights Reserved
  432.  
  433. $Id: rgb.c,v 1.2.1.1 1994/11/16 06:57:48 jason Exp jason $
  434. a6 3
  435.  * Revision 1.2.1.1  1994/11/16  06:57:48  jason
  436.  * added space diffusion
  437.  *
  438. @
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.